using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Jasongozy.Alaton;
namespace Jasongozy.Copor {
    public class CodeSection {
        public string Content;
        public List<CodeSection> SubCodeSections = new List<CodeSection> ();

        public CodeSection ParentSection;

        public string FilePath = "";

        public bool IsFileRoot {
            get {
                return !string.IsNullOrEmpty(FilePath);
            }
        }

        public override string ToString() {
            StringBuilder code = new StringBuilder();
            code.AppendLine(Content);
            foreach (CodeSection cs in SubCodeSections) {
                code.Append(cs.ToString());
            }
            return code.ToString();
        }

        public void WriteToFile() {
            string content = ToString();
            if (!string.IsNullOrEmpty(content) && !string.IsNullOrEmpty(FilePath)) {
                string dic = Path.GetDirectoryName(FilePath);
                if (!Directory.Exists(dic)) Directory.CreateDirectory(dic);
                File.WriteAllText(FilePath, content);
            }
        }
    }
}
